/**
 * gedulemgr.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gedulemgr.h"
#include <QApplication>
#include "model/model.h"
#include "utils/loader/xmlloader.h"
#include "utils/storer/xmlstorer.h"
#include "utils/logging/logmanager.h"
#include "gui/console/askforconsoledlg.h"
#include "gui/qt/mainwindow.h"

GeduleMgr GeduleMgr::m_instance;

const std::string GeduleMgr::VERSION = "Gedule 0.9.0";
const std::string GeduleMgr::LICENSE = "Copyright (C) 2013 Jose Luzón\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions.";

///////////////////////////////////////////////////////////////
GeduleMgr::GeduleMgr() :
    m_model()
{
}

///////////////////////////////////////////////////////////////
GeduleMgr::~GeduleMgr()
{
    // The destruction must be done at GeduleMgr::Stop,
    // so here there is nothing to do.
}

///////////////////////////////////////////////////////////////
GeduleMgr & GeduleMgr::Instance()
{
    return m_instance;
}

///////////////////////////////////////////////////////////////
bool GeduleMgr::Init()
{
    return InitLoggers();
}

///////////////////////////////////////////////////////////////
bool GeduleMgr::InitLoggers()
{
    GET_LOG_MGR().SubscribeConsoleLogger( LOG_LOGGER, "LOG" );
    return true;
}

///////////////////////////////////////////////////////////////
bool GeduleMgr::Stop()
{
    m_model.Clear();
    return true;
}

///////////////////////////////////////////////////////////////
void GeduleMgr::Subscribe( tCallback a_callback, void * a_object, tCallbackId a_callbackId )
{
    m_callbacks[ a_callbackId ].push_back( std::make_pair( a_callback, a_object ) );
}

///////////////////////////////////////////////////////////////
void GeduleMgr::Notify( tCallbackId a_callbackId, void * a_param )
{
    tCallbacks::iterator it    = m_callbacks[ a_callbackId ].begin();
    tCallbacks::iterator itEnd = m_callbacks[ a_callbackId ].end();
    while( it != itEnd )
    {
        ( (*it).first ) ( (*it).second, a_param );
        ++it;
    }
}

///////////////////////////////////////////////////////////////
bool GeduleMgr::OpenModel( const std::string & a_file )
{
    m_model.Clear();

    XmlLoader loader( a_file );
    return loader.Load( m_model );
}

///////////////////////////////////////////////////////////////
bool GeduleMgr::SaveModel( const std::string & a_file )
{
    XmlStorer storer( a_file );
    return storer.Store( m_model );
}

///////////////////////////////////////////////////////////////
const Model & GeduleMgr::GetModel() const
{
    return m_model;
}

///////////////////////////////////////////////////////////////
Model & GeduleMgr::GetModel()
{
    return m_model;
}

///////////////////////////////////////////////////////////////
short GeduleMgr::Run( int argc, char * argv [], RunMode a_mode )
{
    switch( a_mode )
    {
        case GUI_MODE: return RunGUIMode( argc, argv );
        case CONSOLE_MODE:
        case N_RUN_MODES:
        default:
                return RunConsoleMode();
    }
}

///////////////////////////////////////////////////////////////
short GeduleMgr::RunConsoleMode()
{
    std::cout << VERSION << std::endl;
    std::cout << LICENSE << std::endl << std::endl;

    std::string filePath;
    do
    {
        AskForConsoleDlg< std::string > dlg( "Type xml model file path: " );
        filePath = dlg.AskFor();
    }
    while( !OpenModel( filePath ) );

    return EXIT_SUCCESS;
}

///////////////////////////////////////////////////////////////
short GeduleMgr::RunGUIMode( int argc, char * argv [] )
{
    QApplication a( argc, argv );
    MainWindow w;
    w.show();

    return a.exec();
}

